package com.wuyi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuyi.mapper.FollowMapper;
import com.wuyi.mapper.UserMapper;
import com.wuyi.pojo.Follow;
import com.wuyi.pojo.User;
import com.wuyi.pojo.bo.FollowBO;
import com.wuyi.pojo.vo.MyFollowVO;
import com.wuyi.service.FollowService;
import com.zc.enums.YesOrNo;
import com.zc.utils.JSONResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.swing.plaf.basic.BasicEditorPaneUI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName FollowServiceImpl.java
 * @Description 关注粉丝服务实现类
 * @createTime 2021年11月04日 20:28:00
 */
@Service
public class FollowServiceImpl implements FollowService {

    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UserMapper userMapper;


    /**
     * 查询关注列表
     * @param userName
     * @return
     */
    @Transactional( propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo queryFollowList(String userName, Integer type, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Follow.class);
        Example.Criteria criteria = example.createCriteria();
        if (YesOrNo.NO.type == type) {
            criteria.andEqualTo("followUid", userName);
        }else{
            criteria.andEqualTo("followedUid", userName);
        }
        //条件查询的关注粉丝表
        List<Follow> followList = followMapper.selectByExample(example);
        //返回list
        List<MyFollowVO> myFollowVOList = new ArrayList<>();
        //查询全部用户
        List<User> users = userMapper.selectAll();
        List<Follow> followList1 = followMapper.selectAll();
        if (YesOrNo.NO.type == type) {
            //如果查我的关注
            for (Follow follow : followList) {
                for (User user : users) {
                    if (follow.getFollowedUid().equals(user.getUserName())) {
                        MyFollowVO myFollowVO = new MyFollowVO();
                        BeanUtils.copyProperties(user, myFollowVO);
                        Follow follow1 = new Follow();
                        follow1.setFollowedUid(user.getUserName());
                        int i = followMapper.selectCount(follow1);
                        myFollowVO.setFollowId(follow.getFollowId());
                        myFollowVO.setFansNum(i);
                        myFollowVO.setIsFollowed(1);
                        myFollowVOList.add(myFollowVO);
                    }
                }
            }
        }else {
            //如果查我的粉丝
            for (Follow follow : followList) {
                for (User user : users) {
                    if (follow.getFollowUid().equals(user.getUserName())) {
                        MyFollowVO myFollowVO = new MyFollowVO();
                        BeanUtils.copyProperties(user, myFollowVO);
                        Follow follow1 = new Follow();
                        follow1.setFollowedUid(user.getUserName());
                        int i = followMapper.selectCount(follow1);
                        myFollowVO.setFollowId(follow.getFollowId());
                        myFollowVO.setFansNum(i);
                        myFollowVO.setIsFollowed(0);
                        //判断我的粉丝 我是否已经关注
                        for (Follow follow2 : followList1) {
                            if (follow2.getFollowUid().equals(userName) &&
                                    follow2.getFollowedUid().equals(myFollowVO.getUserName())) {
                                myFollowVO.setIsFollowed(1);
                                break;
                            }
                        }

//                        myFollowVO.setIsFollowed(1);
                        myFollowVOList.add(myFollowVO);
                    }
                }
            }



        }
        PageInfo pageInfo = new PageInfo<>(myFollowVOList);
        pageInfo.setList(myFollowVOList);
        return pageInfo;
    }


    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult addFollow(FollowBO followBO) {

        Follow follow = new Follow();
        follow.setFollowUid(followBO.getFollowUserName());
        follow.setFollowedUid(followBO.getFollowedUserName());

        //查询是否已经关注
        Example example = new Example(Follow.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("followUid", followBO.getFollowUserName());
        criteria.andEqualTo("followedUid", followBO.getFollowedUserName());
        int i = followMapper.selectCountByExample(example);
        if (i == 1) {
            return JSONResult.ok("已为关注用户，不用重复关注!");
        }else{
            follow.setCreateTime(new Date());
            follow.setUpdateTime(new Date());
            int res = followMapper.insert(follow);
            if (res != 1) {
                return JSONResult.errorMsg("关注失败！");
            }
        }
        return JSONResult.ok("关注成功！");
    }

    /**
     * 取消关注
     * @param followBO
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult cancelFollow(FollowBO followBO) {

        Example example = new Example(Follow.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("followUid", followBO.getFollowUserName());
        criteria.andEqualTo("followedUid", followBO.getFollowedUserName());
        Follow follow = followMapper.selectOneByExample(example);
        if (null == follow) return JSONResult.errorMsg("没有找到相关数据");
        int res = followMapper.deleteByPrimaryKey(follow.getFollowId());
        if (res != 1) {
            return JSONResult.errorMsg("取消关注失败");
        }
        return JSONResult.ok("取消关注成功");
    }
}
